/**
 * @(#) CondoProxy.as
 *
 * @date		2013. 05. 29.
 * @version		1.0
 * @author		Park, Jutae
 * @description	 콘도신청 Proxy - Proxy
 * 				콘도신청 리스트를 조회하고 저장, 수정, 삭제한다.
 */
package com.skt.common_hr.condoreq.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.condoreq.CondoFacade;
	import com.skt.common_hr.condoreq.model.vo.CondoVO;
	import com.skt.common_hr.condoreq.model.vo.ProfileVO;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;
	import com.wcl.utils.StringUtil;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
		
	public class CondoProxy extends CommonProxy
	{
		public static const NAME:String = "Condoreq";
		public static const LOAD_CONDOCB_COMPLTE:String		= "LOAD_CONDOCB_COMPLTE";
		public static const LOAD_CONDOCB_FAILED:String		= "LOAD_CONDOCB_FAILED";
		
		public static const LOAD_AREACB_COMPLTE:String		= "LOAD_AREACB_COMPLTE";
		public static const LOAD_AREACB_FAILED:String		= "LOAD_AREACB_FAILED";
		
		public static const LOAD_ROOMSCB_COMPLTE:String		= "LOAD_ROOMSCB_COMPLTE";
		public static const LOAD_ROOMSCB_FAILED:String		= "LOAD_ROOMSCB_FAILED";
		
		public static const LOAD_BUSINESSCB_COMPLTE:String		= "LOAD_BUSINESSCB_COMPLTE";
		public static const LOAD_BUSINESSCB_FAILED:String		= "LOAD_BUSINESSCB_FAILED";
		
		public var profile:ProfileVO;
		public var condoList:ArrayCollection;
		public var sinChungList:ArrayCollection;
		public var acMedYear:ArrayCollection;
		public var sRettext:String;
		
		public var readCondoCb:ArrayCollection;
		public var readRoomsCb:ArrayCollection;
		public var readAreaCb:ArrayCollection;
		public var readBusinessCb:ArrayCollection;

		public var readList:ArrayCollection;
		public var causeList:ArrayCollection;
		public var readList2:ArrayCollection;
		public var errCode:int;
		public var errText:String;
		public var url:String;
		
		public var condoListArr:ArrayCollection;
		public var condoFileArr:ArrayCollection;
		
		public function CondoProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		public function load(pernr:String, sDay:Date, eDay:Date):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			params["I_BEGDA"] = DateUtil.toBasicString(sDay);
			params["I_ENDDA"] = DateUtil.toBasicString(eDay);
			
			loadData("Z_HR_RE06", params, loadResult, loadFault); 
		}
		
		
		
		public function insert(rec:CondoVO ,arr:ArrayCollection):void
		{
			
			var params:Object = new Object();
			var paramsI:Object = new Object();
			var tables:Object = new Object();
			var table:Array = new Array();
			var tableFile:Array = new Array();
			
			paramsI["I_ACTIO"] = "SAVE"; 			
			
			params["PERNR"]				= rec.PERNR;
			params["BEGDA"]				= DateUtil.toBasicString(rec.BEGDA);
			params["ENDDA"]				= DateUtil.toBasicString(rec.ENDDA);
			params["CONDO"]				= rec.CONDO;
			params["CAREA"]				= rec.CAREA;
			params["CROOM"]				= rec.CROOM;
			params["PNUMS"]				= rec.PNUMS;
			params["ZZUSE"]				= rec.ZZUSE;
			params["ZBIGO"]				= rec.ZBIGO;
			params["STATS"]				= "";
			params["SRTFD"]				= rec.SRTFD;
			
			table.push(params);
			
			var obj:Object;
			for (var i:Number = 0 ;i <arr.length;i++){
				obj = {};
				obj.FILE_NAME = arr[i].FILE_NAME;
				obj.FILE_EXT = arr[i].FILE_EXT;
				obj.FILE_SIZE = arr[i].FILE_SIZE;
				obj.FILE_URL = arr[i].FILE_URL;
				tableFile.push(obj);
			}
			
			tables["T_IN"] = table;
			tables["ITABF"] = tableFile;
			
			saveData("Z_HR_RE07", paramsI, tables, insertResult, insertFault); 
		}
		
		public function del(pernr:String, rec:CondoVO):void
		{ 
			var params:Object = new Object();
			params["I_ACTIO"] = "DEL"; 			
			var obj:Object = new Object();
			var tables:Object = new Object();
			var table:Array = new Array();
			obj["PERNR"]				= rec.PERNR;
			obj["BEGDA"]				= DateUtil.toBasicString(rec.BEGDA);
			obj["ENDDA"]				= DateUtil.toBasicString(rec.ENDDA);
			obj["CONDO"]				= rec.CONDO;
			obj["CAREA"]				= rec.CAREA;
			obj["CROOM"]				= rec.CROOM;
			obj["PNUMS"]				= rec.PNUMS;
			obj["ZZUSE"]				= rec.ZZUSE;
			obj["ZBIGO"]				= rec.ZBIGO;
			obj["STATS"]				= rec.STATS;
			obj["SRTFD"]				= rec.SRTFD;
			table.push(obj);
			
			tables["T_IN"] = table;

			saveData("Z_HR_RE07", params, tables, delResult, delFault); 
 
		}
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function loadProfile(pernr:String):void
		{
			var param:Object = new Object();
			param["I_PERNR"] = pernr;
			loadData("Z_HR_0008", param, resultProfile, fault); 
		}
		
		public function readLoad(rec:CondoVO):void
		{
			var params:Object = new Object();
			params["I_ACTIO"] = "DIS"; 			
			var obj:Object = new Object();
			var tables:Object = new Object();
			var table:Array = new Array();
			obj["PERNR"]				= rec.PERNR;
			obj["BEGDA"]				= DateUtil.toBasicString(rec.BEGDA);
			obj["ENDDA"]				= DateUtil.toBasicString(rec.ENDDA);
			obj["CONDO"]				= rec.CONDO;
			obj["CAREA"]				= rec.CAREA;
			obj["CROOM"]				= rec.CROOM;
			obj["PNUMS"]				= rec.PNUMS;
			obj["ZZUSE"]				= rec.ZZUSE;
			obj["ZBIGO"]				= rec.ZBIGO;
			obj["STATS"]				= rec.STATS;
			obj["SRTFD"]				= rec.SRTFD;
			table.push(obj);
			
			tables["T_IN"] = table;

			saveData("Z_HR_RE07", params, tables, readResult, fault);  
			
		}
		public function resultProfile(event:ResultEvent):void
		{
			if(retCode==0) {  
				profile = parseProfile(event.result.RESULT[0]);
				facade.sendNotification(CondoFacade.LOAD_PROFILE_COMPLETE, this);
			}
			else {
				trace( "failed to load profile: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				facade.sendNotification(CondoFacade.LOAD_PROFILE_FAILED, this);
			}
		}
		
		public function loadCondoCombo(objDate:Date):void
		{
			var param:Object = new Object();
			param["I_DATUM"] = DateUtil.toBasicString(objDate);
			
			loadData("Z_HR_RE08", param, resultAttach, fault); 
		}
		
		public function loadAreaCombo(objDate:Date, code:String):void
		{
			var params:Object = new Object();
			params["I_DATUM"] = DateUtil.toBasicString(objDate);
			params["I_CONDO"] = code;
			
			loadData("Z_HR_RE09", params, loadAreaResult, loadFault); 
		}
		
		public function loadRoomsCombo(objDate:Date, code:String, code2:String):void
		{
			var params:Object = new Object();
			params["I_DATUM"] = DateUtil.toBasicString(objDate);
			params["I_CONDO"] = code;
			params["I_CAREA"] = code2;
			
			loadData("Z_HR_RE10", params, loadRoomsResult, loadFault); 
		}
		
		public function loadBusinessCombo(objDate:Date):void
		{
			var params:Object = new Object();
			params["I_DATUM"] = DateUtil.toBasicString(objDate);
			
			loadData("Z_HR_RE12", params, loadBusinessResult, loadFault); 
		}
		
		public function loadResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				condoList = parseCondoList(event.result.T_OUT);
//				sinChungList = parseSinchungList(event.result.OTAB2);
//				causeList = parseCause(event.result.OTABT)
				facade.sendNotification(CondoFacade.LOAD_LIST_COMPLETE, this);
			}
			else {
				facade.sendNotification(CondoFacade.LOAD_LIST_FAILED, this);
			}
		}
		
		public function resultAttach(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {
				readCondoCb = event.result["T_CODE"];
				facade.sendNotification(CondoProxy.LOAD_CONDOCB_COMPLTE, this);
			}
			else {
				trace( "failed to 콘도콤보박스");
				url = "";
				facade.sendNotification(CondoProxy.LOAD_CONDOCB_FAILED, this);
			}
		}
		
		public function loadAreaResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				readAreaCb = event.result["T_CODE"];
				facade.sendNotification(CondoFacade.LOAD_AREACB_COMPLTE, this);
			}
			else {
				facade.sendNotification(CondoFacade.LOAD_AREACB_FAILED, this);
			}
		}
		
		public function loadRoomsResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				readRoomsCb = event.result["T_CODE"];
				facade.sendNotification(CondoFacade.LOAD_ROOMSCB_COMPLTE, this);
			}
			else {
				facade.sendNotification(CondoFacade.LOAD_ROOMSCB_FAILED, this);
			}
		}
		
		public function loadBusinessResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				readBusinessCb = event.result["T_CODE"];
				facade.sendNotification(CondoFacade.LOAD_BUSINESSCB_COMPLTE, this);
			}
			else {
				facade.sendNotification(CondoFacade.LOAD_BUSINESSCB_FAILED, this);
			}
		}

		private function fault(event:FaultEvent):void
		{
			trace(event.fault.faultString);
			errCode = -1;
			errText = "호출실패";
			url = "";
		}
		
		private function loadFault(event:FaultEvent):void
		{
			facade.sendNotification(CondoFacade.LOAD_LIST_FAILED, this);
		}
		
		public function readResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {
				condoListArr = parseCondoList(event.result.T_IN);
				condoFileArr = event.result.ITABF; 
				facade.sendNotification(CondoFacade.READ_LIST_COMPLETE, this);
			}
			else {
				trace(event.result["RETCODE"]+" : "+event.result["RETTEXT"]);
			}
		}
		
		public var failedAlert:String; 
		public function insertResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				facade.sendNotification(CondoFacade.INSERT_COMPLETE, this);
			}
			else {
				failedAlert = event.result.RETTEXT;
				facade.sendNotification(CondoFacade.CANCEL_REQUEST_INPUT, this);
			}
		}
		
		public function delResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				facade.sendNotification(CondoFacade.DELETE_COMPLETE, this);
			}
			else {
				facade.sendNotification(CondoFacade.DELETE_FAILED, this);
			}
		}
		
		private function delFault(event:FaultEvent):void
		{
			facade.sendNotification(CondoFacade.DELETE_FAILED, this);
		}
		
		private function insertFault(event:FaultEvent):void
		{
			facade.sendNotification(CondoFacade.INSERT_FAILED, this);
		}
		
		public var updateFailed:String; 
		public function updateResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {   
				facade.sendNotification(CondoFacade.OK_REQUEST_INPUT, this);
			}
			else {
				updateFailed = event.result.RETTEXT; 
				facade.sendNotification(CondoFacade.CANCEL_REQUEST_INPUT, this);
			}
		}

		private function updateFault(event:FaultEvent):void
		{
			facade.sendNotification(CondoFacade.UPDATE_FAILED, this);
		}
		
		private function parseCondoList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr) 
			{
				var vo:CondoVO = new CondoVO();
				vo.AEDTM		= DateUtil.stringToDateTime(rec["AEDTM"]);
				vo.ATIME		= DateUtil.stringToDateTime(rec["ATIME"]);
				vo.BEGDA		= DateUtil.stringToDateTime(rec["BEGDA"]);
				vo.ENDDA		= DateUtil.stringToDateTime(rec["ENDDA"]);
				vo.CAREA		= rec["CAREA"];
				vo.CATXT		= rec["CATXT"];
				vo.CONDO 		= rec["CONDO"];
				vo.CROOM		= rec["CROOM"];
				vo.CRTXT 		= rec["CRTXT"];
				vo.CTEXT		= rec["CTEXT"];
				vo.DATUM		= DateUtil.stringToDateTime(rec["DATUM"]);
				vo.MANDT		= rec["MANDT"];
				vo.PERIO	 	= rec["PERIO"];
				vo.PERNR	 	= rec["PERNR"];
				vo.PNUMS		= rec["PNUMS"];
				vo.STATS		= rec["STATS"];
				vo.GRADE		= rec["GRADE"];
				vo.SRTFD		= rec["SRTFD"];
				
				vo.STATX	 	= rec["STATX"];
				vo.UTEXT		= rec["UTEXT"];
				vo.UZEIT		= DateUtil.stringToDateTime(rec["UZEIT"]);
				vo.ZZUSE		= rec["ZZUSE"];
				vo.ZBIGO		= rec["ZBIGO"];
				vo.REASN		= rec["REASN"];
				// 변경삭제 유무 
				if(vo.STATS == 'A'){
					vo.STATS_F = true;
				} else {
					vo.STATS_F = false;
				}	
				
				retArr.addItem(vo);
			}
			return retArr;
		}
		
//		private function toJsonType(vo:CondoVO):Object {
//			var paramObj:Object = new Object();
//
//			paramObj.AEDTM		= DateUtil.toBasicString(vo.AEDTM);
//			paramObj.ATIME		= DateUtil.toBasicTimeString(vo.ATIME);
//			paramObj.BEGDA		= DateUtil.toBasicString(vo.BEGDA);
//			paramObj.ENDDA		= DateUtil.toBasicString(vo.ENDDA);
//			paramObj.CAREA		= vo.CAREA;		
//			paramObj.CATXT		= vo.CATXT;		
//			paramObj.CONDO		= vo.CONDO;		
//			paramObj.CROOM		= vo.CROOM;	
//			paramObj.CRTXT		= vo.CRTXT;	
//			paramObj.CTEXT		= vo.CTEXT;
//			paramObj.DATUM		= DateUtil.toBasicString(vo.DATUM);
//			paramObj.MANDT		= vo.MANDT;
//			paramObj.PERIO		= vo.PERIO;
//			paramObj.PERNR		= vo.PERNR;
//			paramObj.PNUMS		= vo.PNUMS;
//			paramObj.STATS		= vo.STATS;	
//			paramObj.STATX		= vo.STATX;
//			paramObj.UZEIT		= DateUtil.toBasicTimeString(vo.UZEIT);
//			paramObj.ZZUSE		= vo.ZZUSE;
//			paramObj.ZBIGO		= vo.ZBIGO;	
//			return paramObj;
//		}
		
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 인사기록 Value Object
		 */
		private function parseProfile(retObj:Object):ProfileVO
		{
			var vo:ProfileVO = new ProfileVO();
			vo.PERNR		= StringUtil.trimFront(retObj["PERNR"], "0");
			vo.ENAME		= retObj["ENAME"];
			vo.PHOTO_URL	= retObj["PHOTO_URL"];
			vo.AGE			=retObj["AGE"];				// 나이
			vo.TEL_HOME		=retObj["TEL_HOME"];		// 집전화
			vo.TEL_OFFICE	=retObj["TEL_OFFICE"];		// 회사전화
			vo.TEL_MOBILE1	=retObj["TEL_MOBILE1"];		// 이동전화 1
			vo.TEL_MOBILE2	=retObj["TEL_MOBILE2"];		// 이동전화 2
			vo.FAX			=retObj["FAX"];				// 팩스
			vo.EMAIL_IN		=retObj["EMAIL_IN"];			// 사내메일	
			vo.EMAIL_EX		=retObj["EMAIL_EX"];			// 사외메일
			vo.STAT2T		=retObj["STAT2T"];			// 재직구분
			vo.ORGEH		=retObj["ORGEH"];			// 조직 코드
			vo.ORGTX		=retObj["ORGTX"];			// 부서명
			vo.PKTXT		=retObj["PKTXT"];			// 고용형태
			vo.PERSK		=retObj["PERSK"];			// 직군코드
			vo.PTEXT		=retObj["PTEXT"];			// 직군
			vo.STELL		=retObj["STELL"];			// 직무 코드
			vo.STELLT		=retObj["STELLT"];			// 직무명
			vo.BAND			=retObj["BAND"];				// 밴드코드
			vo.BANDT		=retObj["BANDT"];			// 밴드명
			vo.IDATE		=DateUtil.stringToDateTime(retObj["IDATE"]);	// 입사일
			trace( vo.IDATE );
			vo.ODATE		=DateUtil.stringToDateTime(retObj["ODATE"]);			// 부서배치일
			vo.PDATE		=DateUtil.stringToDateTime(retObj["PDATE"]);// 승진기준일
			vo.ANKEY		=retObj["ANKEY"];			// 근무지코드
			vo.ANKEYT		=retObj["ANKEYT"];			// 근무지명
			vo.MASSG		=retObj["MASSG"];			// 입사경로 코드
			vo.MASSGT		=retObj["MASSGT"];			// 입사경로명
			vo.CTASK		=retObj["CTASK"];			// 포지션 => 직무대신 출력(2010.10.15 김정용과장요청사항)
			
			//2011.10.31
			vo.ORGTX1		=retObj["ORGTX1"];			// 
			vo.JWTXT		=retObj["JWTXT"];			// 
			vo.JCTXT		=retObj["JCTXT"];			// 
			
			vo.CARDT		= DateUtil.stringToDateTime(retObj["CARDT"]);			//경력기준일
			//2012.02.22
			vo.BDESC		=retObj["BDESC"];			//
			
			vo.GBDAT		=DateUtil.stringToDateTime(retObj["GBDAT"]);			// 생년월일
			vo.ADDRE2		=retObj["ADDRE2"];			// 현주소
			vo.EARRT		=retObj["EARRT"];			// 병역
			vo.SERTYT		=retObj["SERTYT"];			// 병역구분			
			return vo;
		}
		
				
	}
}